package com.ruibo.tax.model.base;

import com.ruibo.tax.model.HBJHXKZJBXX;
import org.hibernate.collection.internal.PersistentSet;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/14 10:18
 */
@XmlRootElement(name = "Package")
public class XMLEncodeModel<T> {

    private PackageHead heads = null;//XML报文头

    private List<T> roots = null;//XML报文数据

    public void setHeads(PackageHead heads) {
        this.heads = heads;
    }

    public void setRoots(List<T> roots) {
        this.roots = roots;
    }

    @XmlElement(name = "PackageHead")
    public PackageHead getHeads() {
        return heads;
    }

    @XmlElementWrapper(name = "Data")
    @XmlElement(name = "Record", required = true)
    public List<T> getRoots() {
        return roots;
    }

    /**
     * 产生模型对应的XML数据
     *
     * @param charset 编码
     * @return String
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public String toSendData(Charset charset) throws IllegalArgumentException, IllegalAccessException {
        StringBuilder builder = new StringBuilder();
        builder.append("<?xml version=\"1.0\" encoding=\"").append(charset.displayName()).append("\"?>");
        builder.append("<Package>");
        builder.append("<PackageHead>");
        Class<?> headsclz = heads.getClass();
        Field[] headsfields = headsclz.getDeclaredFields();
        for (Field field : headsfields) {
            field.setAccessible(true);
            if (field.get(heads) != null) {
                builder.append("<").append(field.getName()).append(">");
                builder.append((String) field.get(heads));
                builder.append("</").append(field.getName()).append(">");
            } else {
                System.out.println("XML报文头字段：" + field.getName() + " 为空");
            }
        }
        builder.append("</PackageHead>");
        builder.append("<Data>");

        for (int i = 0; i < roots.size(); i++) {
            builder.append("    <Record index=\"" + (i + 1) + "\">");
            T t = roots.get(i);
            Class<?> aClass = t.getClass();
            Field[] rootsFields = aClass.getDeclaredFields();
            for (Field field : rootsFields) {
                field.setAccessible(true);
                if (field.get(t) != null) {
                    if (field.getType().equals(Set.class)) {
                        builder.append("<").append(field.getName()).append(">");
                        String encodeModel = encodeModel((PersistentSet) field.get(t));
                        builder.append(encodeModel);
                        builder.append("</").append(field.getName()).append(">");
                        continue;
                    } else {
                        builder.append("<").append(field.getName()).append(">");

                        builder.append(field.get(t).toString());
                        builder.append("</").append(field.getName()).append(">");
                    }
                } else {
                    builder.append("<").append(field.getName()).append(">");
                    builder.append("</").append(field.getName()).append(">");
                }
            }
            builder.append(" </Record>");
        }
        builder.append("</Data>");
        builder.append("</Package>");
        return builder.toString();
    }

    private String encodeModel(PersistentSet dataSet) throws IllegalAccessException {
        StringBuilder stringBuilder = new StringBuilder();
        dataSet.stream().forEach(o -> {
            String name = o.getClass().getSimpleName();
            stringBuilder.append("<").append(name).append(">");
            Class<?> aClass = o.getClass();
            Field[] rootsFields = aClass.getDeclaredFields();
            for (Field field : rootsFields) {
                field.setAccessible(true);
                try {
                    if (field.get(o) != null) {
                        if (field.getType().equals(HBJHXKZJBXX.class)) {
                            continue;
                        } else {
                            stringBuilder.append("<").append(field.getName()).append(">");
                            stringBuilder.append(field.get(o).toString());
                            stringBuilder.append("</").append(field.getName()).append(">");
                        }
                    } else {
                        System.out.println("XML报文数据字段：" + field.getName() + " 为空");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            stringBuilder.append("</").append(name).append(">");
        });
        return stringBuilder.toString();
    }


}
